#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/stat.h>
#include <pthread.h>
#include <fcntl.h>
#include <ctype.h>
#include "interface_emum.h"
#include "log.h"
#include "data_handle_lib.h"
#include "ipc_node.h"
#include "usr_xdma.h"
#include "pthread_own.h"
#include "ipc_member.h"
#include "low_speed_port.h"
#include "cmd.h"

#ifdef __cplusplus
extern "C" {
#endif

#define LOG_TAG "data_handle_lib.c :"

#define K_B             (1024UL)
#define M_B             (1024 * 1024UL)
#define BLOCK_ALIGN 4096

typedef struct
{
    long int msg_type;
    data_block_str block;
} msg_st_slow_port;

typedef struct
{
    unsigned int block_size;
    unsigned int total_block_size;
    unsigned int block_num_per_group;
    unsigned int block_number;
    unsigned int position[ENMUM_INTERFACE_MAX - ENMUM_RESERVE];
} big_data_write_type;

typedef struct{
//////////////////////////////////////////////////////////////////////////////////////////////
    int         c2h_channel_count;
    int         c2h_share_memory_id[C2H_CHANNEL_MAX_NUM];
    int         c2h_msg_queue_id[C2H_CHANNEL_MAX_NUM];
    msg_st      c2h_msg_st[C2H_CHANNEL_MAX_NUM];
    char        *c2h_block_buff[C2H_CHANNEL_MAX_NUM];
    char        *c2h_block_buff_aligned[C2H_CHANNEL_MAX_NUM];//the address must be 4096 aliged
    unsigned int c2h_pos[H2C_CHANNEL_MAX_NUM];
/////////////////////////////////////////////////////////////////////////////////////////////
    int         h2c_channel_count;
    int         h2c_share_memory_id[H2C_CHANNEL_MAX_NUM];
    int         h2c_msg_queue_id[H2C_CHANNEL_MAX_NUM];
    msg_st      h2c_msg_st[H2C_CHANNEL_MAX_NUM];
    char        *h2c_block_buff[H2C_CHANNEL_MAX_NUM];
    char        *h2c_block_buff_aligned[H2C_CHANNEL_MAX_NUM];//the address must be 4096 aliged
    unsigned int h2c_pos[H2C_CHANNEL_MAX_NUM];
/////////////////////////////////////////////////////////////////////////////////////////////
    unsigned int dma_channel_number;
    unsigned int user_channel_number;
    int         named_pipe_fd;
    int         block_num;
    uint32_t    block_size;
}xdma_dev_user;

static xdma_dev_user xdma_dev;

static int g_msg_id_array[MSG_ARRAY_SIZE];
static int env_initial_done = 0;
static big_data_write_type shm_block_info;

static int destroy_msg_queue(int id)
{
    int ret_val = -1;
    if (id < 0)
    {
        LOG(LOG_TAG"msgid < 0,check!\r\n");
        return -1;
    }
    ret_val = msgctl(id, IPC_RMID, 0);
    if (ret_val < 0)
    {
        LOGE(LOG_TAG"msgctl(IPC_RMID) msgid failed,errno = %d\r\n", errno);
        return -errno;
    }

    return 0;
}

static int generate_msg_queue(int type,int channel_number)
{
    key_t tmp_key = -1;
    int tmp_id = -1;

    tmp_key = ftok(MSG_QUEUE_KEY_PATH, (type == C2H_READ_DATA) ? MSG_QUEUE_KEY_ID_READ[channel_number] : MSG_QUEUE_KEY_ID_WRIRE[channel_number]);
    if (tmp_key < 0)
    {
        LOGE(LOG_TAG"ftok gen key failed,errno = %d\r\n", errno);
        return -errno;
    }

    tmp_id = msgget(tmp_key, 0666 | IPC_CREAT);
    if (tmp_id < 0)
    {
        LOGE(LOG_TAG"msgget failed,ret_val = %d，errno = %d\r\n",tmp_id,errno);
        return -errno;
    }
    return tmp_id;
}

static int destroy_share_memory(char *memory_ptr, int id)
{
    int ret_val = -1;
    if (!memory_ptr) return -1;

    ret_val = shmdt(memory_ptr);
    if (ret_val < 0)
    {
        LOGE(LOG_TAG"shmdt failed,errno = %d\r\n", errno);
        return -errno;
    }

    ret_val = shmctl(id, IPC_RMID, NULL);
    if (ret_val < 0)
    {
        LOGE(LOG_TAG"shmctl failed,errno = %d\r\n", errno);
        return -errno;
    }

    return 0;
}
//void *generate_share_memory(xdma_usr_dev *dev,int *shmid,int channel_number)
static void *gen_share_memory(unsigned int mem_size, int *id, int type,int channel_number)
{
    key_t tmp_key = -1;
    int tmp_id = -1;
    char *tmp_ptr = NULL;

    tmp_key = ftok(SHARE_MEM_KEY_PATH, (type == C2H_READ_DATA) ? SHARE_MEM_KEY_ID_READ[channel_number] : SHARE_MEM_KEY_ID_WRITE[channel_number]);
    if (tmp_key < 0)
    {
        LOGE(LOG_TAG"ftok gen key failed,errno = %d\r\n", errno);
        return NULL;
    }
    LOGD("ftok tmp_key = 0x%x,mem_size = 0x%x\r\n", tmp_key, mem_size);
    tmp_id = shmget(tmp_key, mem_size, IPC_CREAT | 0666);
    if (tmp_id < 0)
    {
        LOGE(LOG_TAG"shmget failed,errno = %d\r\n", errno);
        return NULL;
    }
    *id = tmp_id;

    tmp_ptr = shmat(tmp_id, NULL, 0);
    if (tmp_ptr < 0)
    {
        LOGE(LOG_TAG"shmat failed,errno = %d\r\n", errno);
        return NULL;
    }

    return tmp_ptr;
}

static int block_if_initial(unsigned int block_num, unsigned int block_size)
{
    int ret_val = -1,i = 0;

    //LOG(LOG_TAG"block_num = %d,block_size = %dKB\r\n", block_num, block_size);

    xdma_dev.named_pipe_fd = open(NAMED_PIPE_PATH, O_RDWR);
    if (xdma_dev.named_pipe_fd < 0)
    {
        LOGE(LOG_TAG"open %s failed,errno = %d\r\n", NAMED_PIPE_PATH, errno);
        return -errno;
    }
    for (i = 0;i < xdma_dev.c2h_channel_count;i++)
    {
        xdma_dev.c2h_msg_queue_id[i] = generate_msg_queue(C2H_READ_DATA, i);
        if (xdma_dev.c2h_msg_queue_id[i] < 0)
        {
            LOGE(LOG_TAG"generate_msg_queue failed,errno = %d\r\n", errno);
            goto block_if_init_err_out;
        }
        
        xdma_dev.c2h_block_buff[i] = gen_share_memory(block_size * block_num * K_B + BLOCK_ALIGN, &(xdma_dev.c2h_share_memory_id[i]), C2H_READ_DATA,i);
        if (!xdma_dev.c2h_block_buff[i])
        {
            LOGE(LOG_TAG"gen_share_memory failed,errno = %d\r\n", errno);
            goto block_if_init_err_out;
        }
        xdma_dev.c2h_block_buff_aligned[i] = ADDRESS_ALIGN(xdma_dev.c2h_block_buff[i], BLOCK_ALIGN);
    }

    for (i = 0;i < xdma_dev.h2c_channel_count;i++)
    {
        xdma_dev.h2c_msg_queue_id[i] = generate_msg_queue(H2C_WRITE_DATA, i);
        if (xdma_dev.h2c_msg_queue_id[i] < 0)
        {
            LOGE(LOG_TAG"generate_msg_queue failed,errno = %d\r\n", errno);
            goto block_if_init_err_out;
        }
        
        xdma_dev.h2c_block_buff[i] = gen_share_memory(block_size * block_num * K_B + BLOCK_ALIGN, &(xdma_dev.h2c_share_memory_id[i]), H2C_WRITE_DATA,i);
        if (!xdma_dev.h2c_block_buff[i])
        {
            LOGE(LOG_TAG"gen_share_memory failed,errno = %d\r\n", errno);
            goto block_if_init_err_out;
        }
        xdma_dev.h2c_block_buff_aligned[i] = ADDRESS_ALIGN(xdma_dev.h2c_block_buff[i], BLOCK_ALIGN);
    }

    return 0;
block_if_init_err_out:
    if (xdma_dev.named_pipe_fd > 0) close(xdma_dev.named_pipe_fd);
    // for (i = 0;i < xdma_dev.c2h_channel_count;i++)
    // {
    //     if (xdma_dev.c2h_block_buff[i])    destroy_share_memory(xdma_dev.c2h_block_buff[i], xdma_dev.c2h_share_memory_id);
    //     if (xdma_dev.c2h_msg_queue_id[i] > 0)    destroy_msg_queue(xdma_dev.c2h_msg_queue_id[i]);
    // }
    // for (i = 0;i < xdma_dev.h2c_channel_count;i++)
    // {
    //     if (xdma_dev.h2c_block_buff[i])    destroy_share_memory(xdma_dev.h2c_block_buff[i], xdma_dev.h2c_share_memory_id);
    //     if (xdma_dev.h2c_msg_queue_id[i] > 0)    destroy_msg_queue(xdma_dev.h2c_msg_queue_id[i]);
    // }

    return -1;
}

static void block_if_deinitial(void)
{
    int i = 0;
    if (xdma_dev.named_pipe_fd > 0)
    {
        close(xdma_dev.named_pipe_fd);
        xdma_dev.named_pipe_fd = -1;
    }
    // for (i = 0;i < xdma_dev.c2h_channel_count;i++)
    // {
    //     if (xdma_dev.c2h_block_buff[i])    destroy_share_memory(xdma_dev.c2h_block_buff[i], xdma_dev.c2h_share_memory_id);
    //     if (xdma_dev.c2h_msg_queue_id[i] > 0)    destroy_msg_queue(xdma_dev.c2h_msg_queue_id[i]);
    // }
    // for (i = 0;i < xdma_dev.h2c_channel_count;i++)
    // {
    //     if (xdma_dev.h2c_block_buff[i])    destroy_share_memory(xdma_dev.h2c_block_buff[i], xdma_dev.h2c_share_memory_id);
    //     if (xdma_dev.h2c_msg_queue_id[i] > 0)    destroy_msg_queue(xdma_dev.h2c_msg_queue_id[i]);
    // }
}


static int cmd_inject(int cmd)
{
    int ret_val = -1, tmp_cmd = cmd;

    if (xdma_dev.named_pipe_fd < 0)
    {
        LOGE(LOG_TAG"cmd_inject channel has been shuted down!\r\n");
        return xdma_dev.named_pipe_fd;
    }

    ret_val = write(xdma_dev.named_pipe_fd, &tmp_cmd, sizeof(tmp_cmd));
    if (ret_val < 0)
    {
        LOGE(LOG_TAG"write failed(cmd_inject),errno = %d\r\n", errno);
        return -errno;
    }
    return 0;
}

int transfer_start(void)
{
    if (cmd_inject(CMD_START) < 0)
    {
        LOGE("transfer_start failed,errno = %d\r\n", errno);
        return -errno;
    }

    return 0;
}

int transfer_stop(void)
{
    if (cmd_inject(CMD_STOP) < 0)
    {
        LOGE("transfer_stop failed,errno = %d\r\n", errno);
        return -errno;
    }

    return 0;
}

int read_data(data_blk_typedef *data_blk)
{
    int t_interface_number = data_blk->block_type, ret_val = -1, read_len = data_blk->block_size;
    msg_st_slow_port t_msg_slow_port;
    mem_block_str    *t_mem_block = (mem_block_str *)(&(t_msg_slow_port.block));

    //LOGD(LOG_TAG"t_interface_number = %d,g_msg_id_array[t_interface_number]:%d,&t_msg_slow_port : %p\r\n",t_interface_number,g_msg_id_array[t_interface_number],&t_msg_slow_port);
    if (t_interface_number >= ENMUM_LOW_SPEED_IO_0 && t_interface_number < ENMUM_RESERVE)
    {
#ifdef USE_PIPE_TO_WRITE_DATA
        ret_val = read(g_msg_id_array[t_interface_number], (void*)(&t_msg_slow_port), sizeof(msg_st_slow_port));
        if (ret_val < 0)
        {
            LOGE(LOG_TAG"read g_msg_id_array[%d] failed,errno = %d\r\n", t_interface_number, errno);
            return -errno;
        }
        //LOG("ret_val = %d\r\n",ret_val);
#else
        ret_val = msgrcv(g_msg_id_array[t_interface_number], (void*)(&t_msg_slow_port), BUFSIZ, C2H_MSG_TYPE, 0);
        if (ret_val < 0)
        {
            LOGE(LOG_TAG"msgrcv failed,errno = %d\r\n", errno);
            return -errno;
        }
#endif
        data_blk->block_type = t_msg_slow_port.block.block_type;
        data_blk->block_size = t_msg_slow_port.block.block_size;
        if (read_len < data_blk->block_size)
        {
            LOGE("block len is too small,need %d,get %d\r\n", data_blk->block_size, read_len);
            return -EINVAL;
        }
        memcpy(data_blk->block_ptr, t_msg_slow_port.block.block, t_msg_slow_port.block.block_size);
    }
    else if(t_interface_number > ENMUM_RESERVE && t_interface_number < ENMUM_INTERFACE_MAX)
    {
        ret_val = msgrcv(xdma_dev.c2h_msg_queue_id[t_interface_number - ENMUM_RESERVE], (void*)(&(xdma_dev.c2h_msg_st[t_interface_number - ENMUM_RESERVE])), BUFSIZ, xdma_dev.c2h_msg_st[t_interface_number - ENMUM_RESERVE].msg_type, 0);
        if (ret_val < 0)
        {
            LOGE(LOG_TAG"read_data msgrcv failed,errno = %d\r\n", errno);
        }
        data_blk->block_type = xdma_dev.c2h_msg_st[t_interface_number - ENMUM_RESERVE].block.block_type;
        data_blk->block_size = xdma_dev.c2h_msg_st[t_interface_number - ENMUM_RESERVE].block.block_size;
        if (read_len < data_blk->block_size)
        {
            LOGE("block len is too small,need %d,get %d\r\n", data_blk->block_size, read_len);
            return -EINVAL;
        }
        memcpy(data_blk->block_ptr, \
               xdma_dev.c2h_block_buff_aligned[t_interface_number - ENMUM_RESERVE] + xdma_dev.block_size * xdma_dev.c2h_msg_st[t_interface_number - ENMUM_RESERVE].block.block_number, \
               xdma_dev.c2h_msg_st[t_interface_number - ENMUM_RESERVE].block.block_size);
    }
    else
    {
        LOGE("read_data,wrong interface_number!\r\n");
        return -EINVAL;
    }

    return data_blk->block_size;
}

int write_data(data_blk_typedef *data_blk)
{
    int t_interface_number = data_blk->block_type, ret_val = -1;
    msg_st_slow_port t_msg_slow_port;
    char t_buff[DEFAULT_MSG_SIZE + 100];
    data_block_str *ptr = (data_block_str *)t_buff;
    unsigned int shm_position = 0;
    //UCAS_DEBUG_LOG;
    //LOGD("write_data ptr->block_type = %d,ptr->block_size = %d\r\n",data_blk->block_type,data_blk->block_size);
    if (t_interface_number >= ENMUM_LOW_SPEED_IO_0 && t_interface_number < ENMUM_RESERVE)
    {
        //UCAS_DEBUG_LOG;
        ptr->block_type = PORT_SEND_DATA;
        ptr->block_size = data_blk->block_size;
        if (ptr->block_size > DEFAULT_MSG_SIZE)
        {
            LOGE("the payload size must < %ld,get %d\r\n", DEFAULT_MSG_SIZE,ptr->block_size);
            return 0;
        }
        //UCAS_DEBUG_LOG;
        memcpy(ptr->block, data_blk->block_ptr, ptr->block_size);
        ret_val = write(g_msg_id_array[t_interface_number + ENMUM_RESERVE], t_buff, sizeof(data_block_str));
        if (ret_val < 0)
        {
            LOGE(LOG_TAG"write  g_msg_id_array[%d] failed,errno = %d\r\n", t_interface_number, errno);
            return -errno;
        }
    }
    else if (t_interface_number > ENMUM_RESERVE && t_interface_number < ENMUM_INTERFACE_MAX)
    {
        //UCAS_DEBUG_LOG;
        shm_position = xdma_dev.h2c_pos[t_interface_number - ENMUM_RESERVE] * xdma_dev.block_size;
        memcpy(xdma_dev.h2c_block_buff_aligned[t_interface_number - ENMUM_RESERVE] + shm_position, data_blk->block_ptr, ptr->block_size);
        //some LOGD
        xdma_dev.h2c_msg_st[t_interface_number - ENMUM_RESERVE].msg_type = H2C_MSG_TYPE;
        xdma_dev.h2c_msg_st[t_interface_number - ENMUM_RESERVE].block.block_type = t_interface_number - ENMUM_RESERVE;
        xdma_dev.h2c_msg_st[t_interface_number - ENMUM_RESERVE].block.block_number = xdma_dev.h2c_pos[t_interface_number - ENMUM_RESERVE];
        xdma_dev.h2c_msg_st[t_interface_number - ENMUM_RESERVE].block.block_size = xdma_dev.block_size;
        ret_val = msgsnd(xdma_dev.h2c_msg_queue_id[t_interface_number - ENMUM_RESERVE], (const void *)(&(xdma_dev.h2c_msg_st[t_interface_number - ENMUM_RESERVE])), sizeof(xdma_dev.h2c_msg_st[t_interface_number - ENMUM_RESERVE].block), 0);
        if (ret_val < 0)
        {
            LOGE(LOG_TAG"msgsnd failed,errno = %d\r\n", errno);
            return -errno;
        }

        xdma_dev.h2c_pos[t_interface_number - ENMUM_RESERVE]++;
    }
    else
    {
        LOGE("write_data,unknown block_type:%d,exit!\r\n",t_interface_number);
        return -EINVAL;
    }
    //UCAS_DEBUG_LOG;
    //UCAS_DEBUG_LOG;
    ptr->block_type = t_interface_number;

    return ptr->block_size;
}

void env_deinitial(void)
{
    int i = 0;
    for (i = 0; i < ENMUM_RESERVE; i++)
    {
        if (g_msg_id_array[i] >= 0)
        {
#ifdef USE_PIPE_TO_WRITE_DATA
            close(g_msg_id_array[i]);
            g_msg_id_array[i] = -1;
#else
            //destroy_msg_queue(g_msg_id_array[i]);
            //g_msg_id_array[i] = -1;
#endif
        }
        if (g_msg_id_array[i + ENMUM_RESERVE] >= 0)
        {
            close(g_msg_id_array[i + ENMUM_RESERVE]);
            g_msg_id_array[i + ENMUM_RESERVE] = -1;
        }
    }
    block_if_deinitial();
    env_initial_done = 0;
}

int env_initial(unsigned int dma_channel_number,unsigned int user_channel_number,unsigned int block_num, unsigned int block_size)
{
    int i = 0, ret_val = -1;
    unsigned char *t_buff = NULL;
    if (env_initial_done)   return 0;

    env_initial_done = 1;
    memset(&xdma_dev,'\0',sizeof(xdma_dev_user));
    xdma_dev.c2h_channel_count = dma_channel_number;
    xdma_dev.h2c_channel_count = dma_channel_number;

    xdma_dev.user_channel_number = user_channel_number;
    xdma_dev.block_num = block_num;
    xdma_dev.block_size = block_size;
    for (i = 0; i < MSG_ARRAY_SIZE; i++)
    {
        g_msg_id_array[i] = -1;
    }

    if (!block_num || !block_size)
    {
        LOGE(LOG_TAG"wrong argument block_num:%d(!=0),block_size:%d(!=0)\r\n", block_num, block_size);
        return -EINVAL;
    }

#ifdef USE_PIPE_TO_WRITE_DATA
    t_buff = (unsigned char *)malloc(M_B);
    if (!t_buff) goto env_initial_err;
    for (i = 0; i < ENMUM_RESERVE; i++)
    {
        g_msg_id_array[i] = open(ucas_msg_path[i], O_RDWR | O_NONBLOCK); //named pipe for c2h
        if (g_msg_id_array[i] < 0)
        {
            LOGE(LOG_TAG"open pipe for c2h i = %d failed,errno = %d\r\n", i, errno);
            goto env_initial_err;
        }
        while (1)
        {
            ret_val = read(g_msg_id_array[i], t_buff, M_B);
            if (ret_val <= 0)
            {
                LOGD("pipe fd = %d is cleared,ret_val = %d!\r\n", g_msg_id_array[i], ret_val);
                break;
            }
            else
                LOGD("try to clear pipe fd = %d,get %d data\r\n", g_msg_id_array[i], ret_val);
        }

    }
    for (i = 0; i < ENMUM_RESERVE; i++)
    {
        if (g_msg_id_array[i] >= 0)
        {
            close(g_msg_id_array[i]);
            g_msg_id_array[i] = -1;
        }
    }
    if (t_buff)
    {
        free(t_buff);
        t_buff = NULL;
    }
#endif

    for (i = 0; i < ENMUM_RESERVE; i++)
    {
#ifdef USE_PIPE_TO_WRITE_DATA
        g_msg_id_array[i] = open(ucas_msg_path[i], O_RDWR); //named pipe for read
        if (g_msg_id_array[i] < 0)
        {
            LOGE(LOG_TAG"open pipe for c2h i = %d failed,errno = %d\r\n", i, errno);
            goto env_initial_err;
        }
#else
        g_msg_id_array[i] = generate_msg_queue(ucas_msg_path[i], i); //msg for c2h
        if (g_msg_id_array[i] < 0)
        {
            LOGE(LOG_TAG"generate_msg_queue i = %d failed,errno = %d\r\n", i, errno);
            goto env_initial_err;
        }
        //LOGD("msg_id[%d]:%d\r\n",i,g_msg_id_array[i]);
#endif
        g_msg_id_array[i + ENMUM_RESERVE] = open(ucas_pipe_path[i], O_RDWR); //named pipe for write
        if (g_msg_id_array[i + ENMUM_RESERVE] < 0)
        {
            LOGE(LOG_TAG"open pipe for h2c i = %d failed,errno = %d\r\n", i, errno);
            goto env_initial_err;
        }
        //LOGD("pipe_fd[%d]:%d\r\n",i + ENMUM_RESERVE,g_msg_id_array[i + ENMUM_RESERVE]);
    }

    ret_val = block_if_initial(block_num, block_size);
    if (ret_val < 0)
    {
        LOGE(LOG_TAG"block_if_initial faild,errno = %d!\r\n", errno);
        goto env_initial_err;
    }

    return 0;
env_initial_err:
    env_deinitial();
    return -errno;
}

int interface_initial(data_blk_typedef *data_blk)
{
    int t_interface_number = data_blk->block_type, ret_val = -1;
    msg_st_slow_port t_msg_slow_port;
    char t_buff[DEFAULT_MSG_SIZE];
    data_block_str *ptr = (data_block_str *)t_buff;

    if (t_interface_number >= ENMUM_RESERVE) return -EINVAL;

    ptr->block_type = PORT_SET_PARAM;
    ptr->block_size = data_blk->block_size;
    if (ptr->block_size > DEFAULT_MSG_SIZE)
    {
        LOGE("the payload size must < %ld,get %d\r\n", DEFAULT_MSG_SIZE,ptr->block_size);
        return -EINVAL;
    }
    memcpy(ptr->block, data_blk->block_ptr, ptr->block_size);

    ret_val = write(g_msg_id_array[t_interface_number + ENMUM_RESERVE], t_buff, sizeof(data_block_str));
    if (ret_val < 0)
    {
        LOGE(LOG_TAG"write  g_msg_id_array[%d] failed,errno = %d\r\n", t_interface_number, errno);
        return -errno;
    }
    ptr->block_type = t_interface_number;

    return 0;
}

int update_flash_write(data_blk_typedef *data_blk)
{
    int t_interface_number = data_blk->block_type, ret_val = -1;
    msg_st_slow_port t_msg_slow_port;
    char t_buff[DEFAULT_MSG_SIZE];
    data_block_str *ptr = (data_block_str *)t_buff;

    ptr->block_type = t_interface_number;
    ptr->block_size = data_blk->block_size;
    if (ptr->block_size > DEFAULT_MSG_SIZE)
    {
        LOGE("the payload size must < %ld,get %d\r\n", DEFAULT_MSG_SIZE,ptr->block_size);
        return -EINVAL;
    }
    memcpy(ptr->block, data_blk->block_ptr, ptr->block_size);

    ret_val = write(g_msg_id_array[(ENMUM_RESERVE - 1) + ENMUM_RESERVE], t_buff, sizeof(data_block_str));
    if (ret_val < 0)
    {
        LOGE(LOG_TAG"write  g_msg_id_array[%d] failed,errno = %d\r\n", (ENMUM_RESERVE - 1), errno);
        return -errno;
    }
    ptr->block_type = t_interface_number;
    return 0;
}

int interface_deinitial(int if_type)
{
    return 0;
}

#ifdef __cplusplus
}
#endif
